package com.xiaoma.registrar;

import java.util.Map;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author maj
 * @ClassName MyAutoConfiguredMyBatisRegistrar
 * @Description
 * @date 2020-09-26 16:42
 */
public class MyAutoConfiguredMyBatisRegistrar implements ImportBeanDefinitionRegistrar,
    ResourceLoaderAware {

  private ResourceLoader resourceLoader;

  @Override
  public void setResourceLoader(ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
  }

  /**
   *
   * @param importingClassMetadata 被注解类元信息
   * @param registry
   */
  @Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
      BeanDefinitionRegistry registry) {


    Map<String, Object> annotationAttributes = importingClassMetadata
        .getAnnotationAttributes(MyServiceScan.class.getName());

    if (annotationAttributes!=null) {
      //得到MyServiceScan注解中的扫描路径属性值
      String basePackage = (String) annotationAttributes.get("basePackage");

      // 当前MyClassPathBeanDefinitionScanner已被修改为扫描带有指定注解的类
      MyClassPathBeanDefinitionScanner scanner =
          new MyClassPathBeanDefinitionScanner(registry, false);

      scanner.setResourceLoader(resourceLoader);
      scanner.registerFilters(MyService.class);//指定要扫描的注解
      scanner.doScan(basePackage);
    }

  }
}
